package net.tarine.gastronomi.client.widgets;

import net.tarine.gastronomi.client.AuthSingleton;
import net.tarine.gastronomi.client.HistoryMgr;
import net.tarine.gastronomi.client.IAuthenticatedWidget;
import net.tarine.gastronomi.shared.AppConst;
import net.tarine.gastronomi.shared.Role;
import net.tarine.gastronomi.shared.to.GasTo;
import net.tarine.gastronomi.shared.to.GasUserTo;

import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;

public class MenuPanel extends FlowPanel implements IAuthenticatedWidget {

	private GasUserTo user = null;
	private GasTo gas = null;
	
	private Image logo;
	private FlowPanel gasPanel = null;
	private Hyperlink gasLink = null;
	private Tree tree;
	private Tree adminTree;
	
	public MenuPanel() {
		AuthSingleton.get().authorizeWidget(this);
	}
	@Override
	public void unlockWidget(GasUserTo user) {
		this.user = user;
		draw();
	}
	
	private void draw() {
		this.clear();
		logo = new Image("img/gastronomi.png");
		this.add(logo);
		//Pannello gas
		gasPanel = new FlowPanel();
		this.add(gasPanel);
		if (gas != null) drawGasPanel();
		//Pannello utente
		FlowPanel userPanel = new FlowPanel();
		String userName = "";
		if (user.getRoleId().intValue() == Role.ADMIN.getId()) {
			userName += "<img src='img/application-certificate.png' style='vertical-align:middle' />&nbsp;";
		} else {
			userName += "<img src='img/single-user.png' style='vertical-align:middle' />&nbsp;";
		}
		if (user.getNickname() != null) {
			userName += "<b>"+user.getNickname()+"</b>";
		}
		if (!user.getEmail().equalsIgnoreCase(user.getNickname()) ) {
			userName += "&nbsp;("+user.getEmail()+")";
		}
		HTML nicknameLabel = new HTML(userName);
		userPanel.add(nicknameLabel);
		Anchor logout = AuthSingleton.get().getLogoutAnchor();
		userPanel.add(logout);
		this.add(userPanel);
		tree = new Tree();
		//Discussioni
		TreeItem discussioniItem = new TreeItem("<img src='img/internet-group-chat.png' style='vertical-align:middle' /> <b>Discussioni</b>");
		Hyperlink listDisc = new Hyperlink("Ultime discussioni", HistoryMgr.FORUM_THREAD_FIND);
		Hyperlink creaDisc = new Hyperlink("Crea discussione",
				HistoryMgr.FORUM_THREAD_EDIT+HistoryMgr.SEPARATOR_TOKEN+
				HistoryMgr.PARAM_ID+HistoryMgr.SEPARATOR_VALUES+AppConst.NEW_ITEM_ID);
		discussioniItem.addItem(listDisc);
		discussioniItem.addItem(creaDisc);
		tree.addItem(discussioniItem);
		discussioniItem.setState(true);
		//Ordini
		TreeItem ordiniItem = new TreeItem("<img src='img/accessories-text-editor.png' style='vertical-align:middle' /> <b>Ordini</b>");
		HTML listOrd = new HTML("Ultimi ordini");
		HTML creaOrd = new HTML("Crea ordine");
		ordiniItem.addItem(listOrd);
		ordiniItem.addItem(creaOrd);
		tree.addItem(ordiniItem);
		ordiniItem.setState(true);
		//Calendario
		TreeItem calendarioItem = new TreeItem("<img src='img/x-office-calendar.png' style='vertical-align:middle' /> <b>Calendario</b>");
		HTML listCal = new HTML("Prossimi eventi");
		HTML creaCal = new HTML("Crea evento");
		calendarioItem.addItem(listCal);
		calendarioItem.addItem(creaCal);
		tree.addItem(calendarioItem);
		calendarioItem.setState(true);
		//Impostazioni
		TreeItem impostazioniItem = new TreeItem("<img src='img/preferences-desktop-theme.png' style='vertical-align:middle' /> <b>Preferenze</b>");
		HTML persPref = new HTML("Personali");
		impostazioniItem.addItem(persPref);
		tree.addItem(impostazioniItem);
		impostazioniItem.setState(true);
		this.add(tree);
		//Amministrazione
		adminTree = new Tree();
		if (new Integer(Role.ADMIN.getId()).equals(user.getRoleId())) {
			TreeItem adminItem = new TreeItem("<img src='img/system-users.png' style='vertical-align:middle' /> <b>Amministrazione</b>");
			Hyperlink gasUsers = new Hyperlink("Utenti", true, HistoryMgr.GAS_USER_FIND);
			adminItem.addItem(gasUsers);
			gasLink = new Hyperlink("Gas", true, HistoryMgr.GAS_EDIT);
			adminItem.addItem(gasLink);
			adminItem.setState(true);
			adminTree.addItem(adminItem);
		}
		this.add(adminTree);
	}
	
	private void drawGasPanel() {
		if (gasPanel != null) {
			gasPanel.clear();
			gasPanel.add(new InlineHTML("GAS: <b>"+gas.getName()+"</b>"));
		}
	}
	private void drawGasLink() {
		if (gasLink != null) {
			gasLink.setHTML("Gas "+gas.getName());
			gasLink.setTargetHistoryToken(HistoryMgr.GAS_EDIT+HistoryMgr.SEPARATOR_TOKEN+
					AppConst.PARAM_ID+HistoryMgr.SEPARATOR_VALUES+gas.getId());
		}
	}
	
	public void setGas(GasTo gas) {
		this.gas = gas;
		drawGasPanel();
		drawGasLink();
	}

}
